<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
  <head>
    <title>仓库类型列表</title>
    <script type="text/javascript">
      var newWarehouseTypeWin;
      var editWarehouseTypeWin;
      var warehouseTypeId;
      var warehouseTypeDS;

      //处理销售类型锁定
      function lockWarehouseTypeFn(id){
        Ext.Ajax.request({
          url : '/warehouse_type/warehouseTypeLock',
          success : function (result, request) {
            warehouseTypeDS.load();
          },
          failure : function (result, request){
            Ext.MessageBox.show({
              title: '消息',
              msg: "通讯失败，请从新操作",
              buttons: Ext.MessageBox.OK,
              icon: Ext.MessageBox.WARNING
            });
          },
          method : 'GET',
          params : {
            id : id
          }
        });
      }
      
      Ext.onReady(function(){
        //仓库类型设置---------------------------------------------------------
        //生成有关仓库类型的Grid
        warehouseTypeDS =  Ext.create('Ext.data.Store', {
          autoDestroy : true,
          autoSync : true,
          model : 'WarehouseType',
          proxy : {
            type : 'ajax',
            url :  '/warehouse_type/getAllWarehouseTypes',
            reader : {
              type : 'json',
              root : 'root',
              totalProperty : 'totalProperty'// 数据集记录总数
            }
          },
          pageSize : 1000,//default:25
          autoLoad : true
        });

        //锁定仓库类型
        function renderWarehouseTypeIsLucked(value, cellmeta, record, index, columnIndex, store){
          if (record.get("is_locked")=="1"){
            return "<a style=cursor:pointer onclick=lockWarehouseTypeFn(" + store.getAt(index).get('id')+")><em style=color:red>锁定</em></a>";
          }else{
            return "<a style=cursor:pointer onclick=lockWarehouseTypeFn(" + store.getAt(index).get('id')+")><em style=color:green>未锁定</em></a>";
          }
        }
        
        var warehouseTypeGrid =  Ext.create('Ext.grid.Panel', {
          store: warehouseTypeDS,
          columns : [Ext.create('Ext.grid.RowNumberer'),{
            header: 'id',
            dataIndex: 'id',
            width: 30
          }, {
            header: '名称',
            dataIndex: 'name',
            width:130
          }, {
            header: '是否锁定',
            dataIndex: 'is_locked',
            renderer: renderWarehouseTypeIsLucked,
            width: 60
          },{
            header: '生成日期',
            dataIndex: 'created_at',
            renderer: Ext.util.Format.dateRenderer('Y-m-d H:i'),
            width: 115
          },{
            header: '修改日期',
            dataIndex: 'updated_at',
            renderer: Ext.util.Format.dateRenderer('Y-m-d H:i'),
            width: 115
          }],
          selModel :Ext.create('Ext.selection.CheckboxModel'),
          width: screenWidth-165,
          height: screenHeight-285,
          iconCls: 'icon-grid',
          loadMask: true,
          //collapsible: true,
          //animCollapse: false,
          //frame: true,
          //title: '仓库类型列表',
          bbar: Ext.create('Ext.PagingToolbar', {
            pageSize: 10,
            store: warehouseTypeDS,
            displayInfo: true,
            displayMsg: "显示第 {0} 条到 {1} 条记录，一共 {2} 条",
            emptyMsg: "没有记录"
          }),
          tbar: [{
              text: '添加',
              iconCls: 'addItem',
              handler : function(){
                newWarehouseTypeWin = Ext.create('Ext.window.Window', {
                  layout: 'fit',
                  width:327,
                  height:112,
                  closeAction: 'destroy',
                  plain: true,
                  modal: true,
                  constrain:true,
                  //modal: true,
                  title: '添加仓库类型',
                  autoLoad: {
                    url: "/warehouse_type/newWarehouseType",
                    scripts: true
                  }
                });
                newWarehouseTypeWin.on("destroy",function(){
                  warehouseTypeDS.load();
                });
                newWarehouseTypeWin.resizable = false;
                newWarehouseTypeWin.show();
              }
            },"-",{
              text: '编辑',
              iconCls: 'editItem',
              handler : function(){
                var records = warehouseTypeGrid.getSelectionModel().getSelection();
                if(records.length==0){
                  Ext.MessageBox.show({
                    title: '提示信息',
                    msg: "请先选中一条记录后，再编辑。",
                    buttons: Ext.MessageBox.OK,
                    icon: Ext.MessageBox.WARNING
                  });
                }else{
                  //把表单添加到窗口中
                  warehouseTypeId = records[0].get('id');
                  editWarehouseTypeWin = Ext.create('Ext.window.Window', {
                    title: '编辑仓库类型',
                    layout:'fit',
                    width:327,
                    height:112,
                    closeAction:'destroy',
                    constrain:true,
                    plain: true,
                    modal: true,
                    autoLoad: {
                      url: "/warehouse_type/editWarehouseType?id=" + warehouseTypeId,
                      scripts: true
                    }
                  });
                }
                editWarehouseTypeWin.on("destroy",function(){
                  warehouseTypeDS.load();
                });
                editWarehouseTypeWin.resizable = false;
                editWarehouseTypeWin.show();
              }
            }]
        });
        warehouseTypeGrid.render('warehouse_type_list');
      })
    </script>
  </head>
  <body>
    <div id="warehouse_type_list"></div>
  </body>
</html>
